/**
 * 
 */
package com.justspeak.util.xml;

import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.R.integer;

import com.justspeak.module.CourseInfo;
import com.justspeak.module.UnitInfo;

/**
 * @author Administrator
 *
 */
public class UnitListContentHandle extends DefaultHandler  {

    private List<UnitInfo> unitInfos=null;
    
    private UnitInfo unitInfo=null;
    
    

    private String tagName=null;
    
    /**
     * @param unitInfos
     */
    public UnitListContentHandle(List<UnitInfo> unitInfos) {
	super();
	this.unitInfos = unitInfos;
    }
    
    /**
     * 
     */
    public UnitListContentHandle() {
	super();
    }

    @Override
    public void characters(char[] ch, int start, int length)
	    throws SAXException {
	String node=new String(ch,start,length);
        if(tagName.equals("id")){
            unitInfo.setId(node);
        }else if(tagName.equals("book")){
            unitInfo.setBook(node);
        }else if(tagName.equals("title")){
            unitInfo.setTitle(node);
        }else if(tagName.equals("course_count")){
            unitInfo.setCourseCount(Integer.valueOf(node));
        }
    }

    @Override
    public void endDocument() throws SAXException {
	// TODO Auto-generated method stub
	super.endDocument();
    }

    @Override
    /** 
     * 重写endElement方法，元素结束时，讲UnitInfo对象添加到列表中
     **/
    public void endElement(String uri, String localName, String qName)
	    throws SAXException {
	if(qName.equals("resource")){
	           unitInfos.add(unitInfo);
	       }
	       tagName="";
    }

    @Override
    public void startDocument() throws SAXException {
	// TODO Auto-generated method stub
	super.startDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
	    Attributes attributes) throws SAXException {
	tagName=localName;
	      if(tagName.equals("resource")){
	          unitInfo=new UnitInfo();
	      }
    }

    public List<UnitInfo> getUnitInfos() {
        return unitInfos;
    }

    public void setUnitInfos(List<UnitInfo> unitInfos) {
        this.unitInfos = unitInfos;
    }

    public UnitInfo getUnitInfo() {
        return unitInfo;
    }

    public void setUnitInfo(UnitInfo unitInfo) {
        this.unitInfo = unitInfo;
    }

  

}
